<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24" style="height: 45px">
            <a-form-item>
              <a @click="">
                ┃ 基本信息
              </a>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入姓名"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerId_dictText">
              <j-popup
                v-model="model.customerId_dictText"
                field="customerId"
                org-fields="id,cust_full_name"
                dest-fields="customerId,customerId_dictText"
                code="mdm_customer_pop"
                :multi="false"
                @input="popupCallback"
                :disabled="customerDisabled"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="手机" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mobile">
              <a-input v-model="model.mobile" placeholder="请输入手机号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="homeAddress">
              <a-input v-model="model.homeAddress" placeholder="请输入地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sex">
              <j-dict-select-tag type="list" v-model="model.sex" dictCode="sex" placeholder="请选择性别" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="属性" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="prop">
              <j-dict-select-tag type="list" v-model="model.prop" dictCode="crm_contacts_prop" placeholder="请选择属性" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dept">
              <a-input v-model="model.dept" placeholder="请输入部门"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="职务" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="post">
              <a-input v-model="model.post" placeholder="请输入职务"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="2" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" style="height: 45px">
            <a-form-item>
              <a @click="handleOthterInfo">
                ┃ 其它信息
                <a-icon :type="showOtherInfo ? 'up' : 'down'" />
              </a>
            </a-form-item>
          </a-col>
          <template v-if="showOtherInfo">
            <a-col :span="12">
              <a-form-model-item label="民族" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nation">
                <j-search-select-tag v-model="model.nation" dict="sys_dict_item where dict_id = '1491717855924199426',item_text,item_value"  />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="年龄段" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ageGroup">
                <j-dict-select-tag type="list" v-model="model.ageGroup" dictCode="crm_age_group" placeholder="请选择年龄段" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="微信" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="wechat">
                <a-input v-model="model.wechat" placeholder="请输入微信"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="QQ" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qq">
                <a-input v-model="model.qq" placeholder="请输入QQ"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="telephone">
                <a-input v-model="model.telephone" placeholder="请输入电话"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="email">
                <a-input v-model="model.email" placeholder="请输入邮箱"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="兴趣爱好" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hobby">
                <a-textarea v-model="model.hobby" rows="2" placeholder="请输入兴趣爱好" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="性格特征" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="trait">
                <a-textarea v-model="model.trait" rows="2" placeholder="请输入性格特征" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="忌讳事项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taboo">
                <a-textarea v-model="model.taboo" rows="2" placeholder="请输入忌讳事项" />
              </a-form-model-item>
            </a-col>
          </template>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MdmCustomerContactsForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      initAddModel: {
        type: Object
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           name: [
              { required: true, message: '请输入姓名!'},
           ],
           customerId_dictText: [
              { required: true, message: '请选择客户!'},
           ],
           mobile: [
              { required: false},
              { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},
             { validator: (rule, value, callback) => validateDuplicateValue('mdm_customer_contacts', 'mobile', value, this.model.id, callback)},
           ],
           email: [
              { required: false},
              { pattern: /^([\w]+\.*)([\w]+)@[\w]+\.\w{3}(\.\w{2}|)$/, message: '请输入正确的电子邮件!'},
           ],
        },
        url: {
          add: "/mdm/customerContacts/add",
          edit: "/mdm/customerContacts/edit",
          queryById: "/mdm/customerContacts/queryById"
        },
        showOtherInfo: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
      customerDisabled(){
        return this.initAddModel && this.initAddModel.hasOwnProperty('customerId');
      }
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(Object.assign({},this.model, this.initAddModel)));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(value,row){
         this.model = Object.assign(this.model, row);
      },
      handleOthterInfo(){
        this.showOtherInfo = !this.showOtherInfo;
      },
    },
  }
</script>